package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.domain.chronicdis.Chronicdis;
import com.gxuwz.medical.domain.medical.Medical;
import com.gxuwz.medical.vo.PageBean;

public class MedicalServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
			try {
				if(method.equals("list")){
					list(request,response);
				}else if(method.equals("add")){
					add(request,response);
				}else if(method.equals("del")){
					del(request,response);
				}else if(method.equals("edit")){
					edit(request,response);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Medical m = new Medical();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = m.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	
	private void edit(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
		String path = "/page/medical/medical_list.jsp";
		String msg = "添加失败";
		String jgbm=request.getParameter("jgbm");
		String zzjgbm=request.getParameter("zzjgbm");
		String jgmc=request.getParameter("jgmc");
		String dqbm=request.getParameter("dqbm");
		String areacode=request.getParameter("areacode");
		String lsgx=request.getParameter("lsgx");
		String jgjb=request.getParameter("jgjb");
		String sbddlx=request.getParameter("sbddlx");
		String pzddlx=request.getParameter("pzddlx");
		String ssjjlx=request.getParameter("ssjjlx");
		String wsjgdl=request.getParameter("wsjgdl");
		String wsjgxl=request.getParameter("wsjgxl");
		String zgdw=request.getParameter("zgdw");
		String kysjstr=request.getParameter("kysj");//日期
		String frdb=request.getParameter("frdb");
		String zczjnum=request.getParameter("zczj");//数值
		//时间格式转换
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
		Date kysj = formatter.parse(kysjstr, new ParsePosition(0));
		double zczj=Double.parseDouble(zczjnum);
		Medical m=new Medical(jgbm, zzjgbm, jgmc, dqbm, areacode, lsgx, jgjb, sbddlx, pzddlx, ssjjlx, wsjgdl, wsjgxl, zgdw, kysj, frdb, zczj);
		m.update();
		jump(request,response,path);
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String path = "/page/medical/medical_list.jsp";
		String jgbm = request.getParameter("jgbm");
		jgbm = new String(jgbm.getBytes("ISO-8859-1"),"UTF-8");
		Medical m = new Medical();
		m.del(jgbm);
		jump(request,response,path);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		// TODO Auto-generated method stub
		String path = "/page/medical/medical_list.jsp";
		String msg = "添加失败";
		String jgbm=request.getParameter("jgbm");
		String zzjgbm=request.getParameter("zzjgbm");
		String jgmc=request.getParameter("jgmc");
		String dqbm=request.getParameter("dqbm");
		String areacode=request.getParameter("areacode");
		String lsgx=request.getParameter("lsgx");
		String jgjb=request.getParameter("jgjb");
		String sbddlx=request.getParameter("sbddlx");
		String pzddlx=request.getParameter("pzddlx");
		String ssjjlx=request.getParameter("ssjjlx");
		String wsjgdl=request.getParameter("wsjgdl");
		String wsjgxl=request.getParameter("wsjgxl");
		String zgdw=request.getParameter("zgdw");
		String kysjstr=request.getParameter("kysj");//日期
		String frdb=request.getParameter("frdb");
		String zczjnum=request.getParameter("zczj");//数值
		//时间格式转换
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
		Date kysj = formatter.parse(kysjstr, new ParsePosition(0));
		double zczj=Double.parseDouble(zczjnum);
		Medical m=new Medical(jgbm, zzjgbm, jgmc, dqbm, areacode, lsgx, jgjb, sbddlx, pzddlx, ssjjlx, wsjgdl, wsjgxl, zgdw, kysj, frdb, zczj);
		m.add();
		jump(request,response,path);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String path = "/page/medical/medical_list.jsp";
		Medical m = new Medical();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = m.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

}
